package com.hqyj.ssm_project.controller;

import com.hqyj.ssm_project.pojo.Function;
import com.hqyj.ssm_project.pojo.User;
import com.hqyj.ssm_project.pojo.User_Role;
import com.hqyj.ssm_project.service.FunctionService;
import com.hqyj.ssm_project.service.RoleService;
import com.hqyj.ssm_project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private FunctionService functionService;
    @RequestMapping("/login.do")
    public String login(User user, Model model, HttpServletRequest request) {
        User result = userService.login(user);
        if (result!=null) {
//            User_Role user_role = roleService.seleteUser_RoleByUserId(result.getUserId());
//            Integer roleId = user_role.getRoleId();
//            List<Function> functionList=functionService.selectFunctionByRoleId(roleId);
            List<Function> functionList= functionService.selectFunctionByUser(result);
            System.out.println(functionList);
            HttpSession session = request.getSession();
            model.addAttribute("functionList1",functionList);
            model.addAttribute("functionList",functionList);
            session.setAttribute("userSession",result);
            return "index";
        }else {
            model.addAttribute("msg","用户名或密码错误");
            return "login1";
        }
    }
}